<?php
/**
 * Created by PhpStorm.
 * User: yys
 * Date: 2016/11/23
 * Time: 15:28
 */

namespace App\Services\PreferentialService;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PreferentialBackService
{
    public function __construct()
    {
    }
    private function generateId(){
        return date('YmdHis') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /****
     * 保存优惠信息到数据库
     * @param array $data
     * @return bool
     */
    public function savePreferentialToDB(array $data){
        try{
            $preferential['preferential_price']=$data['preferential_price'];
            $preferential['resources_id']=$data['resources_id'];
            $preferential['resources_type']=$data['resources_type'];
            $preferential['begin_time']=$data['begin_time'];
            $preferential['end_time']=$data['end_time'];

            $time=time();
            $preferential['state']=1;
            $preferential['created_at']=$time;
            $preferential['updated_at']=$time;
            DB::beginTransaction();
            $num = DB::table('preferentials')->insert($preferential);
            return ($num==1)?true:false;
        }catch (\Exception $e){
            DB::rollback();
            Log::info($e);
            return false;
        }finally{
            DB::commit();
        }
    }
}